﻿using CatClient;
using CatClient.Message.Spi;
using CatClient.Message.Spi.Heartbeat;
using CatClient.Message.Spi.Heartbeat.Extend;
using CatClient.Util;
using System;
using System.Text;

namespace CatClient.Message.Spi.Internals
{

    public class StatusUpdateTask
    {
        internal NodeStatusInfo _nodeInfo;

        private readonly IMessageStatistics _statistics;

        private bool _inited;

        private object _locker = new object();

        public StatusUpdateTask(IMessageStatistics statistics)
        {
            this._statistics = statistics;
        }

        // Token: 0x06000188 RID: 392 RVA: 0x00006B38 File Offset: 0x00004D38
        private void Init()
        {
            try
            {
                this._nodeInfo = new NodeStatusInfo(this._statistics);
                this._nodeInfo.HeartbeatExtensions.Add(new CpuInfo());
                this._nodeInfo.HeartbeatExtensions.Add(new NetworkIO());
                this._nodeInfo.HeartbeatExtensions.Add(new DiskIO());
                this._nodeInfo.HeartbeatExtensions.Add(new ConnectionInfo());
                this._nodeInfo.HeartbeatExtensions.Add(new ProcessIO());
                this._nodeInfo.Refresh();
                this._nodeInfo.HaveAcessRight = true;
                LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("[StatusUpdateTask 初始化成功]");
                this._inited = true;
            }
            catch (Exception ex)
            {
                LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error("[StatusUpdateTask 初始化失败]" + ex.Message, ex);
            }
        }

        // Token: 0x06000189 RID: 393 RVA: 0x00006C2C File Offset: 0x00004E2C
        public void Run(object o)
        {
            if (!this._inited)
            {
                object locker = this._locker;
                lock (locker)
                {
                    if (!this._inited)
                    {
                        this.Init();
                    }
                }
            }
            for (; ; )
            {
                MilliSecondTimer.SleepToNextMinute(5000);
                if (CatConfig.Config.Domain.Enabled)
                {
                    LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("[StatusUpdateTask loop in.]");
                    if (!this._nodeInfo.HaveAcessRight)
                    {
                        break;
                    }
                    if (!Cat.IsInitialized())
                    {
                        LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info("Cat haven't init!");
                    }
                    else
                    {
                        Cat.DoTransaction("System", "Status", delegate ()
                        {
                            this._nodeInfo.Refresh();
                            string nameValuePairs = XmlHelper.XmlSerialize(this._nodeInfo, Encoding.UTF8);
                            Cat.GetProducer().LogHeartbeat("Heartbeat", AppEnv.IP, PureCatConstants.SUCCESS, nameValuePairs);
                            Cat.GetProducer().LogEvent("System", string.Format("PureCat.Version : {0}", Cat.Version), PureCatConstants.SUCCESS, Cat.Version);
                        }, delegate (Exception e)
                        {
                            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error("Heartbeat Error: {0} ", e);
                        });
                    }
                }
            }
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Warn("Cat couldn't acess right now!");
        }
    }
}
